<?php
/**
 * This class will generate the xhtml code of a selectbox.
 *
 * @author Redouane Benkaddour <redouane@indago.nl>
 * @version 0.5.2
 * @package reform
 * @subpackage select_generator
 */
 
class select_generator extends select_attributes {
	
	/**
    * generates the xhtml code and returns the string
    *
    * @return string $code
    */
	protected function generateCode(){

		$code = '<select ';
		foreach($this->attribute AS $attribute => $value){
			if(isset($this->attribute['multiple']) && $attribute == 'name'){
				$value = $value.'[]';
			}
		$code.= $attribute.'="'.$value.'" ';
		}
		$code.= '>';
		
		foreach($this->_values AS $optgroup => $values){
    		if($optgroup){
    			$code.= '<optgroup label="'.$optgroup.'">';
    		}
			
    		if (sizeof($values) > 0) {
		
    			foreach($values AS $key => $value){
    				$code.= '<option ';
    				if($this->use_key != FALSE){
    					$code.= 'value="'.$key.'" ';
    				}
					
    				if(isset($_REQUEST[$this->attribute['name']])){
    					if(is_array($_REQUEST[$this->attribute['name']])){
    						if($this->use_key != FALSE){
    							if(in_array($key, $_REQUEST[$this->attribute['name']])){
    								$code.='selected="selected"';
    							}
    						}
                            else{
    							if(in_array($value, $_REQUEST[$this->attribute['name']])){
    								$code.='selected="selected"';
    							}
    						} 
    					}
    					else{
    						if($this->use_key != FALSE){
    							if($_REQUEST[$this->attribute['name']] == $key){
    								$code.='selected="selected"';
    							}
    						}
    						else{
    							if($_REQUEST[$this->attribute['name']] == $value){
    								$code.='selected="selected"';
    							}
    						}
    					}
    				}else{
    					if($this->use_key != FALSE){
    						if(in_array($key, $this->selected)){
    							$code.='selected="selected"';
    						}
    					}
    					else{
    						if(in_array($value, $this->selected)){
    							$code.='selected="selected"';
    						}
    					}
    				}
    				$code.= '>'.$value.'</option>';					
    			}
    		}
    		if($optgroup){
    			$code.= '</optgroup>';
    		}
    	}
		$code.= '</select>';
	    return $code;
	}
}

?>